package com.smartrm.globalinfrcore.aggreate;

import com.smartrm.globalinfrcore.api.CommonError;
import com.smartrm.globalinfrcore.event.DomainEvent;
import com.smartrm.globalinfrcore.event.DomainEventBus;
import com.smartrm.globalinfrcore.exception.DomainException;

/*
 * @program: common-starters
 * @description: 聚合基类
 * @author: houhong
 * @create: 2023-01-10 15:08
 */
public abstract class BaseAggregate {


    public DomainEventBus domainEventBus;

    protected long version;
    /*
     * 主动自加
     */
    private boolean versionInc = false;

    protected void incVersion() {
        version++;
        versionInc = true;
    }

    public long getVersion() {
        return version;
    }

    public boolean isVersionInc() {
        return versionInc;
    }

    public DomainEventBus getDomainEventBus() {
        return domainEventBus;
    }

    public void setEventBus(DomainEventBus domainEventBus) {
        this.domainEventBus = domainEventBus;
    }

    /*
     * 发布领域事件
     */
    protected void emitEvent(DomainEvent event) {
        if (domainEventBus != null) {
            domainEventBus.post(event);
        } else {
            throw new DomainException(CommonError.NoEventBus);
        }
    }
}